<script>
	var Towxml = require("./towxml/main.js"),
		uti = require("./we7/resource/js/util.js");

	export default {
		globalData: {
			siteInfo: require("./siteinfo.js"),
			data: {
				appscene: 123,
				is_sq: 0
			},
			userInfo: null,
			isok: false,
			unionid: null,
			util: require("./we7/resource/js/util.js"),
			getuid: function(e) {
				var i = wx.getStorageSync("uid");
				if (i) return i;
				uti.getUserInfo(function(e) {
					if (e.memberInfo) {
						var i = e.memberInfo.uid;
						return wx.setStorageSync("uid", i), i;
					}

					return "";
				});
			},
			imgUrl: function() {
				var that = this;
				var url = '';
				that.util.request({
					url: "entry/wxapp/Api",
					data: {
						m: "monai_market",
						r: "home.index.domain_name"
					},
					cachetime: "0",
					success: function(e) {
						console.log('域名返回：', e.data);
						url = e.data.data;
						return wx.setStorageSync('webUrl', url), url;
					},
					fail: function() {
						return '';
					}
				});
			},
			getAttr: function(e, i) {
				return e.currentTarget.dataset[i];
			},
			back: !1,
			toDate: function(e) {
				var i = new Date(1e3 * e);
				return i.getFullYear() + "." + ((i.getMonth() + 1 < 10 ? "0" + (i.getMonth() + 1) : i.getMonth() + 1) +
					".") + (i.getDate() < 10 ? "0" + i.getDate() : i.getDate());
			},
			upimgs: function(u) {
				var s = this,
					e = u || {};

				if (wx.showLoading({
						title: "正在上传..."
					}), e == {} || !e) {
					if (!u.fail || "function" != typeof u.fail) return !1;
					u.fail({
						errno: 500,
						message: "缺少必要参数"
					});
				}

				var i = e.scannums ? e.scannums : 1,
					o = e.filename ? e.filename : "images",
					f = s.util.url("entry/wxapp/Api", {
						m: "monai_market",
						uid: s.getuid(),
						r: "upimg"
					});
				wx.chooseImage({
					count: i,
					sizeType: ["compressed"],
					sourceType: ["album", "camera"],
					success: function(e) {
						wx.showLoading({
							title: "正在上传..."
						});
						var a = e.tempFilePaths,
							n = (e.tempFiles, new Array(a.length)),
							r = 0;

						for (var i in a) {
							! function(t) {
								wx.uploadFile({
									url: f,
									filePath: a[t],
									name: "file",
									formData: {
										names: o
									},
									success: function(e) {
										var i = JSON.parse(e.data);
										return 0 != i.errno ? (s.util.message({
											title: i.message,
											type: "error"
										}), void wx.hideLoading()) : (r++, n[t] = i.data,
											r == a.length && u.success && "function" ==
											typeof u.success ? (u.success(n), void wx
												.hideLoading()) : void 0);
									},
									fail: function(e) {
										wx.hideLoading();
										var i = JSON.parse(e.data);
										u.fail && "function" == typeof u.fail && u.fail(i);
									}
								});
							}(i);
						}

						setTimeout(function() {
							wx.hideLoading();
						}, 5e3);
					},
					fail: function() {
						wx.hideLoading(), u.fail && "function" == typeof u.fail && u.fail();
					}
				});
			},
			upimg: function(t) {
				var a = this,
					e = a.util.url("entry/wxapp/Api", {
						m: "monai_market",
						uid: a.getuid(),
						r: "upimg"
					}),
					i = t.filename ? t.filename : "images";
				wx.uploadFile({
					url: e,
					filePath: t.img,
					name: "file",
					formData: {
						names: i
					},
					success: function(e) {
						console.log(e);
						var i = JSON.parse(e.data);
						return 0 != i.errno ? (a.util.message({
								title: i.message,
								type: "error"
							}), void wx.hideLoading()) : i.data && t.success && "function" == typeof t
							.success ? (t.success(i.data), void wx.hideLoading()) : void 0;
					},
					fail: function(e) {
						console.log(e), wx.hideLoading();
						var i = JSON.parse(e.data);
						t.fail && "function" == typeof t.fail && t.fail(i);
					}
				});
			},
			delimg: function(i) {
				var e = i.uid ? i.uid : "";
				if ("" == e) return i.fail && "function" == typeof i.fail && i.fail({
					errno: 500,
					message: "请允许获取用户信息"
				}), !1;
				var t = i.url ? i.url : "";
				if ("" == t) return i.fail && "function" == typeof i.fail && i.fail({
					errno: 500,
					message: "图片路径错误"
				}), !1;
				this.util.request({
					url: "entry/wxapp/Api",
					data: {
						m: "monai_market",
						uid: e,
						imgurl: t,
						r: "upimg.delImg"
					},
					cachetime: "0",
					success: function(e) {
						i.success && "function" == typeof i.success && i.success();
					},
					fail: function() {
						i.fail && "function" == typeof i.fail && i.fail();
					}
				});
			},
			delseverimg: function(e) {
				this.util.request({
					url: "entry/wxapp/Api",
					data: {
						m: "monai_sharing",
						imgurl: e,
						r: "Upimg.delserverImg"
					}
				});
			}
		},
		onLaunch: function(e) {
			// 获取位置
			this.globalData.data.appscene = e.scene;
			/* 已授权之后，自动获取用户信息 */
			// 判断是否授权

			wx.getSetting({
				success: res => {
					//箭头函数为了处理this的指向问题 
					if (res.authSetting["scope.userInfo"]) {
						console.log("已授权");
						this.globalData.data.is_sq = 1; // 获取用户信息

						wx.getUserInfo({
							success: res => {
								//箭头函数为了处理this的指向问题
								this.globalData.isok = true;
								var that = this;
								console.log(res.userInfo); //用户信息结果

								wx.getStorage({
									key: 'unionid',

									success(res) {
										that.globalData.unionid = res.data;
									}

								});
								this.globalData.userInfo = res.userInfo;

								if (this.userInfoReadyCallback) {
									//当index.js获取到了globalData就不需要回调函数了，所以回调函数需要做做一个判断，如果app.js中有和这个回调函数，那么就对这个函数进行调用，并将请求到的结果传到index.js中
									this.userInfoReadyCallback(res.userInfo);
								}
							}
						});
					} else {
						console.log("未授权");
						wx.removeStorage({
							key: 'unionid'
						});
					}
				}
			});
		},
		onShow() {
			
		},
	
		methods: {}
	};
</script>
<style>
	@import "./app.css";
	@import '/static/style/iconfont.css';
</style>
